package com.vcc.service.impl;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryChain;
import com.mybatisflex.core.update.UpdateChain;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.vcc.core.exception.NotFoundException;
import com.vcc.core.exception.ProgramException;
import com.vcc.data.entity.WhiteLabel;
import com.vcc.data.mapper.WhiteLabelMapper;
import com.vcc.service.boss.response.whiteLabel.WhiteLabelVoBoss;
import com.vcc.service.sa.BossUserUtil;
import com.vcc.service.service.WhiteLabelService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import static com.vcc.data.entity.table.BossUserTableDef.BOSS_USER;
import static com.vcc.data.entity.table.WhiteLabelTableDef.WHITE_LABEL;

@Service
public class WhiteLabelServiceImpl extends ServiceImpl<WhiteLabelMapper, WhiteLabel> implements WhiteLabelService {
    @Override
    public WhiteLabel getByDomain(String domain) {
        return queryChain().eq("domain", domain).oneOpt().orElseThrow(
                () -> new NotFoundException("没有找到当前信息")
        );
    }

    @Override
    public WhiteLabel getByBossUserId(Long bossUserId) {
        return queryChain().eq("boss_user_id", bossUserId).oneOpt().orElseThrow(
                () -> new NotFoundException("没有找到当前信息")
        );
    }

    @Override
    public Boolean existsByDomain(String domain) {
        return queryChain().eq("domain", domain).exists();
    }

    @Override
    public Boolean existsByAdminDomain(String adminDomain) {
        return queryChain().eq("admin_domain", adminDomain).exists();
    }

    @Override
    public WhiteLabel getByAdminDomain(String adminDomain) {
        return queryChain().eq(WhiteLabel::getAdminDomain, adminDomain).oneOpt().orElseThrow(() -> new NotFoundException("没有找到对应的白标订单"));
    }

    @Override
    public Boolean existsByBossUserId(Long bossUserId) {
        return queryChain().eq("boss_user_id", bossUserId).exists();
    }

    @Override
    public Page<WhiteLabelVoBoss> pageing(String name, String domain, Long bossUserId, Integer status, Integer pageNumber, Integer pageSize) {
        QueryChain<WhiteLabel> queryChain = queryChain();
        queryChain.leftJoin(BOSS_USER).on(WHITE_LABEL.BOSS_USER_ID.eq(BOSS_USER.ID));
        queryChain.select(WHITE_LABEL.ALL_COLUMNS);
        queryChain.select(BOSS_USER.EMAIL.as("account"));
        if (StringUtils.isNotBlank(name)) {
            queryChain.like("name", name);
        }
        if (StringUtils.isNotBlank(domain)) {
            queryChain.like("domain", domain);
        }
        if (status != null) {
            queryChain.eq("status", status);
        }
        if (bossUserId != null) {
            queryChain.eq("boss_user_id", bossUserId);
        }
        queryChain.orderBy(WHITE_LABEL.CREATE_DATE.desc());
        return pageAs(
                Page.of(
                        pageNumber == null ? 1 : pageNumber,
                        pageSize == null ? 20 : pageSize
                ),
                queryChain,
                WhiteLabelVoBoss.class
        );
    }


    @Override
    public Boolean audit(Long id, Boolean status, String remark) {
        WhiteLabel whiteLabel = getByIdOpt(id).orElseThrow(() -> new NotFoundException("没有找到对应的白标订单"));
        if (whiteLabel.getStatus() != 2) {
            throw new ProgramException("订单不在审核状态无法审核");
        }
        UpdateChain<WhiteLabel> updateChain = updateChain();
        updateChain.set(WhiteLabel::getStatus, status ? 0 : 1);
        updateChain.set(WhiteLabel::getRemark, remark);
        updateChain.set(WhiteLabel::getOperatorId, BossUserUtil.getId());
        updateChain.eq(WhiteLabel::getId, id);
        return updateChain.update();
    }

    @Override
    public Boolean updateStatus(Long id, Integer status) {
        WhiteLabel whiteLabel = getByIdOpt(id).orElseThrow(() -> new NotFoundException("没有找到对应的白标订单"));
        UpdateChain<WhiteLabel> updateChain = updateChain();
        updateChain.set(WhiteLabel::getStatus, status);
        updateChain.set(WhiteLabel::getOperatorId, BossUserUtil.getId());
        updateChain.eq(WhiteLabel::getId, whiteLabel.getId());
        return updateChain.update();
    }
}
